<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>MongoDB 结合 PHP | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>MongoDB 结合 PHP</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MongoDB/">MongoDB</a>
			</span>
		
	</div>

	

	
		<h2 id="1-安装-PHP-的-MongoDB-扩展"><a href="#1-安装-PHP-的-MongoDB-扩展" class="headerlink" title="1.安装 PHP 的 MongoDB 扩展"></a>1.安装 PHP 的 MongoDB 扩展</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@primary ~]<span class="comment"># wget 'https://codeload.github.com/mongodb/mongo-php-driver/zip/master' -O /usr/local/src/mong_php.zip</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@primary ~]<span class="comment"># cd /usr/local/src</span></div><div class="line">[root@primary src]<span class="comment"># unzip mong_php.zip </span></div><div class="line">[root@primary src]<span class="comment"># cd mongo-php-driver-master/</span></div><div class="line">[root@primary mongo-php-driver-master]<span class="comment"># /usr/local/php/bin/phpize</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;出错无法解决，重新下载包</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@primary ~]<span class="comment"># wget http://pecl.php.net/get/mongo-1.4.5.tgz</span></div><div class="line">[root@primary ~]<span class="comment"># tar xfv mongo-1.4.5.tgz </span></div><div class="line">[root@primary ~]<span class="comment"># cd mongo-1.4.5</span></div><div class="line">[root@primary mongo-1.4.5]<span class="comment"># /usr/local/php/bin/phpize </span></div><div class="line">[root@primary mongo-1.4.5]<span class="comment"># ./configure --with-php-config=/usr/local/php/bin/php-config</span></div><div class="line">[root@primary mongo-1.4.5]<span class="comment"># make &amp;&amp; make install</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑 /usr/local/php/etc/php.ini </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@primary mongo-1.4.5]<span class="comment"># vim /usr/local/php/etc/php.ini</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加 extension = mongo.so</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">extension = mongo.so</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E7%BB%93%E5%90%88%20PHP/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/usr/local/php/bin/php –m</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@primary mongo-1.4.5]<span class="comment"># /usr/local/php/bin/php -m</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E7%BB%93%E5%90%88%20PHP/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启apache，重新加载php</p>
<h2 id="2-测试-MongoDB-扩展"><a href="#2-测试-MongoDB-扩展" class="headerlink" title="2.测试 MongoDB 扩展"></a>2.测试 MongoDB 扩展</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑 /usr/local/apache2/htdocs/1.php </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@primary ~]<span class="comment"># vim /usr/local/apache2/htdocs/1.php</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">// 连接到mongodb</div><div class="line"><span class="variable">$m</span> = new MongoClient();</div><div class="line"><span class="built_in">echo</span> <span class="string">"Connection to database successfully"</span>;</div><div class="line">// 选择一个数据库</div><div class="line"><span class="variable">$db</span> = <span class="variable">$m</span>-&gt;mydb;</div><div class="line"><span class="built_in">echo</span> <span class="string">"Database mydb selected"</span>;</div><div class="line">?&gt;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E7%BB%93%E5%90%88%20PHP/03.png?raw=true" alt=""></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@primary ~]<span class="comment"># curl localhost/1.php</span></div><div class="line">Connection to database successfullyDatabase mydb selected</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;参考<a href="https://hcldirgit.github.io/2017/08/27/MongoDB/8.%20MongoDB%20PHP/" target="_blank" rel="external">MongoDB PHP</a></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/MongoDB/6. MongoDB 结合 PHP/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
